//工程化
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
//创建实例

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue')
    },
    {
      name: 'mypersonal',
      path: '/mypersonal/:id',
      component: () => import('@/views/mypersonal.vue')
    },
    {
      name: 'mypersonalEdit',
      path: '/mypersonalEdit/:id',
      component: () => import('@/views/mypersonalEdit.vue')
    },
    {
      name: 'articleDetail',
      path: '/articleDetail/:id',
      component: () => import('@/views/articleDetail.vue')
    },
    {
      name: 'myfollows',
      path: '/myfollows',
      component: () => import('@/views/myfollows.vue')
    },
    {
      name: 'myuserStar',
      path: '/myuserStar',
      component: () => import('@/views/myuserStar.vue')
    },
    {
      name: 'comment',
      path: '/comment/:id',
      component: () => import('@/views/comment.vue'),
      props: true
    },
    {
      name: 'program',
      path: '/program',
      component: () => import('@/views/program.vue'),
      props: true
    },

    {
      name: 'search',
      path: '/search',
      component: () => import('@/views/search.vue'),
    }

  ]
});

const arr = ['mypersonal', 'mypersonalEdit'];
//导航守卫
router.beforeEach((to, from, next) => {
  if (arr.indexOf(to.name) != -1) {
    const token = localStorage.getItem('userKey');
    if (token) {
      next();
    } else {
      next('/login')
    }
  } else {
    next();
  }

})





//暴露
export default router
